package com.skydonkey.config;


import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.cache.annotation.CachingConfigurerSupport;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.cache.RedisCacheConfiguration;
import org.springframework.data.redis.cache.RedisCacheManager;
import org.springframework.data.redis.cache.RedisCacheWriter;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.RedisSerializationContext;
import org.springframework.data.redis.serializer.StringRedisSerializer;

import java.time.Duration;
import java.util.HashMap;
import java.util.Map;


@Configuration
public class RedisConfig extends CachingConfigurerSupport {

    public static final String DECOLLATOR = ":";

    public static final String APP_PREFIX = "skydonkey";

    public static final String USER_CACHE_NAME = "user"; //用户

    public static final String CACHE_NAMES_PREFIX = APP_PREFIX + DECOLLATOR;

    public static final String CACHE_NAME_FOREVER = CACHE_NAMES_PREFIX + "forever";

    public static final String CACHE_NAME_MINUTES_10 = CACHE_NAMES_PREFIX + "minutes-10";

    public static final String CACHE_NAME_MINUTES_30 = CACHE_NAMES_PREFIX + "minutes-30";

    public static final String CACHE_NAME_HOURS_01 = CACHE_NAMES_PREFIX + "hours-1";

    public static final String CACHE_NAME_HOURS_24 = CACHE_NAMES_PREFIX + "hours-24";

    public static final String CACHE_NAME_HOURS_12 = CACHE_NAMES_PREFIX + "hours-12";

    public static final String CACHE_NAME_DAYS_30 = CACHE_NAMES_PREFIX + "days-30";


    @Bean
    @SuppressWarnings("all")
    public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory factory, Jackson2JsonRedisSerializer jackson2JsonRedisSerializer) {
        RedisTemplate<String, Object> template = new RedisTemplate<>();
        template.setConnectionFactory(factory);

        StringRedisSerializer stringRedisSerializer = new StringRedisSerializer();
        template.setKeySerializer(stringRedisSerializer);
        template.setHashKeySerializer(stringRedisSerializer);
        template.setValueSerializer(jackson2JsonRedisSerializer);
        template.setHashValueSerializer(jackson2JsonRedisSerializer);
//        template.setValueSerializer(stringRedisSerializer);
//        template.setHashValueSerializer(stringRedisSerializer);
        template.afterPropertiesSet();
        return template;
    }

    @Bean
    public Jackson2JsonRedisSerializer<Object> getJsonRedisSerializer() {
        Jackson2JsonRedisSerializer<Object> jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer<>(Object.class);
        ObjectMapper om = new ObjectMapper();
        om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
        om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
        jackson2JsonRedisSerializer.setObjectMapper(om);
        return jackson2JsonRedisSerializer;
    }


    @Bean
    public RedisCacheManager cacheManager(RedisConnectionFactory connectionFactory) {
        //设置不同cacheName的过期时间
        Map<String, RedisCacheConfiguration> configurations = new HashMap<>(16);
        // 序列化方式
        Jackson2JsonRedisSerializer<Object> jsonRedisSerializer = getJsonRedisSerializer();
        RedisSerializationContext.SerializationPair<Object> serializationPair =
                RedisSerializationContext.SerializationPair.fromSerializer(jsonRedisSerializer);
        // 默认的缓存时间
        Duration defaultTtl = Duration.ofSeconds(20L);
        // 用户模块的缓存时间
        Duration userTtl = Duration.ofSeconds(50L);
        // 默认的缓存配置
        RedisCacheConfiguration redisCacheConfiguration = RedisCacheConfiguration.defaultCacheConfig()
                //.entryTtl(defaultTtl)
                .serializeValuesWith(serializationPair);
        // 自定义用户模块的缓存配置 自定义的配置可以覆盖默认配置（当前的模块）
        configurations.put(RedisConfig.APP_PREFIX, RedisCacheConfiguration.defaultCacheConfig()
                //.entryTtl(userTtl)
                .serializeValuesWith(serializationPair)
        );

        return RedisCacheManager.builder(RedisCacheWriter.nonLockingRedisCacheWriter(connectionFactory))
                .cacheDefaults(redisCacheConfiguration)
                .withInitialCacheConfigurations(configurations)
                // 事物支持
                .transactionAware()
                .build();
    }
}
